package com.wc.alorithm_blue_bridge._DFS.Q4494;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/11/27-22:24
 */
public class Main {
    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();
        // 为了构建树
        HashMap<Integer, Node> map = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            int num = sc.nextInt();
            Node node = new Node();
            node.val = num;
            map.put(i, node);
        }
        // 为了找到哪一个是树根
        boolean[] isHead = new boolean[n + 1];
        Arrays.fill(isHead, true);

        for (int i = 1; i <= n; i++) {
            int leftId = sc.nextInt();
            int rightId = sc.nextInt();
            Node curNode = map.get(i);
            if (leftId != -1) {
                isHead[leftId] = false;
                curNode.leftChild = map.get(leftId);
            }
            if (rightId != -1) {
                isHead[rightId] = false;
                curNode.rightChild = map.get(rightId);
            }
        }

        //找到 树根
        Node root = new Node();
        for (int i = 1; i <= n; i++) {
            if (isHead[i]) {
                root = map.get(i);
            }
        }
        dfs(root, 0);
        System.out.println(sum);
    }

    static int sum = 0;

    static public void dfs(Node root, int curNum) {
        if (root == null) {
            return;
        }
        if (curNum == 0) {
            sum += root.val;
        }

        dfs(root.leftChild, curNum + 1);
        dfs(root.rightChild, curNum - 1);
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}

class Node {
    int val;
    Node leftChild;
    Node rightChild;
}

